<!-- Form to fill in from what order no somethign is returning -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <fieldset>
        <table>
            <tr>
                <td class="bold" width="70px">Order No.: </td>
                <td><input type="text" name="order_no"/></td><!-- the value from this is important and needs to be given in all submits -->
            </tr>
            <tr>
                <td><input type="submit" name="submit_order_no" value="" class="ok" title="Return order"/></td>
            </tr>
        </table>
    </fieldset>
</form>
<?php
function setVariables($id, $return){
    //set all the other information in the array
    $array['name'] = $_POST['name'];
    $array['type'] = $_POST['type'];
    $array['id'] = $id;
    $array['cat'] = $_POST['cat'];
    $array['pno'] = $_POST['pno'];
    $array['price'] = $_POST['price'];
    $array['discount'] = $_POST['discount'];
    $array['VAT'] = $_POST['VAT'];
    $array['max'] = $_POST['amount'];
    $array['return'] = $return;
    $i = 0;
    //check if the $i is set, go to line that doesn't exist in the session
    while (isset($_SESSION['return_list'][$i])) { 
        $i++;
    }
    //put the value's on new session line
    $_SESSION['return_list'][$i] = $array; 
}
//if the empty button is clicked empty the session
if(isset($_POST['empty_return'])){
    if (isset($_SESSION['return_list'])) {
        unset($_SESSION['return_list']);
    }
}

//if delete button is clocked remove product from session
if(isset($_POST['removep'])){
    $idline = $_POST['removep'];
    unset($_SESSION['return_list'][$idline]);
}

//if there is a new order number filled in empty the session return_list
if(isset($_POST['submit_order_no'])){
    if(isset($_SESSION['return_list'])){
        unset($_SESSION['return_list']);
    }
}
if(isset($_POST['update_return'])){
    $line = $_POST['line'];
    $_SESSION['return_list'][$line]['id'] = $_POST['id'];
    $_SESSION['return_list'][$line]['pno'] = $_POST['pno'];
    $_SESSION['return_list'][$line]['name'] = $_POST['name'];
    $_SESSION['return_list'][$line]['type'] = $_POST['type'];
    $_SESSION['return_list'][$line]['price'] = $_POST['price'];
    $_SESSION['return_list'][$line]['discount'] = $_POST['discount'];
    $_SESSION['return_list'][$line]['VAT']= $_POST['VAT'];
    $return = $_POST['return'];
    $max = $_POST['amount'];
    //check if amount is not more than amount
    if ($return > $max) {
    $_SESSION['return_list'][$line]['return'] = $max;
    } else {
    $_SESSION['return_list'][$line]['return'] = $return;
    }
    unset($_SESSION['update_return']);
}
if(isset($_POST['return_p'])){ //something is added to the return list
    //check if something is filled in if not, nothing is done
    if(!empty($_POST['return'])){  
        $return = $_POST['return']; //what is filled in to return
        $max = $_POST['amount']; //the maximum amount that can be returned
        $id = $_POST['id']; //the id of the product or part
        //Return may not be higher than max
        if($return > $max){
            //return is higher. This is not possible, return is set to the maximum amount
            $return_right = $max;
        } else {
            //return was the same or lower than max.
            $return_right = $return;
        }
        //check if the filled in item is already in the session (if session exists)
        if(isset($_SESSION['return_list'])){
            $inlist = false; //standard inlist is false
            foreach($_SESSION['return_list'] as $value){ //go through list
                //if id is the same AND category is the same the product or part is already in the list
                if($id == $value['id']){
                    $inlist = true; //the product is already in the session
                    break;//get out of for each loop
                }//close if
            }//close foreach
            if($inlist == false){
                setVariables($id, $return_right);
            }
        } else {//close checking id session exists 
        setVariables($id,$return_right);
        }
    }//close checking if there is something filled in
}

//check if any of the following buttons is true: submit_order_no, return_p, empty_return_list, update_return_list
if(isset($_POST['submit_order_no']) || isset($_POST['return_p']) || isset($_POST['empty_return']) || isset($_POST['update_return']) || isset($_POST['removep'])){
    //check if there is a order no filled in
    if(!empty($_POST['order_no'])){
        //save the order no to a variable
        $order_no = $_POST['order_no'];
        //check if the order no exists in the database
        $order_no_query = _OrderTable::getInstance()->findOneBy('id', $order_no); //get the order no from the table
        if($order_no_query != null){ //if not null order is in table

            //get all products and parts on the order
            $items = OrderlineTable::findbyOrder($order_no_query);
            
            //get the name of the contact
            $contact = ContactTable::getInstance()->findOneBy('id', $order_no_query->getContactId());
            ?>
            <!-- The form that submits all information to database -->
            <form action="<?php echo url_for('sales/' . ($form->getObject()->isNew() ? 'createReturnOrder' : 'updateOrder') . (!$form->getObject()->isNew() ? '?id=' . $form->getObject()->getId() : '?id=' . $order_no)) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
                <fieldset>
                    <legend class="bold">General</legend>
                    <!-- The general information of the order, info taken from database -->
                    <table>
                        <tr>
                            <td class="bold" width="80px">Order No.:</td>
                            <td width="100px"><?php echo $order_no_query->getId() ?></td>
                            <td class="bold" width="115px">Contact order No.:</td>
                            <td><?php echo $order_no_query->getCOrderNumber() ?></td>
                        </tr>
                        <tr>
                            <td class="bold">Contact No.:</td>
                            <td><?php echo $order_no_query->getContactId() ?></td>
                            <td class="bold">Contact Name:</td>
                            <td><?php echo $contact->getName() ?></td>
                        </tr>
                    </table>
                </fieldset>
                <fieldset>
                    <!-- List of all products and parts on the order -->
                    <legend class="bold">On order</legend>
                    <div id="scroll" class="table_div"><!-- so the list is in a certain area and will scroll if it gets too long -->
                        <table id="return_order">
                            <tr class="bold head_small">
                                <!-- each data cell has a certain width so it displays nicely -->
                                <td width="60px">Item No.</td>
                                <td width="">Name</td>
                                <td width="">Type</td>
                                <td width="">Price per Unit (excl. VAT)</td>
                                <td width="35px">VAT(%)</td>
                                <td width="50px">Quantity</td>
                                <td width="50px">Discount</td>
                                <td width="80px">Total</td>
                                <td width="45px">Return</td>
                                <td width="20px"></td>
                            </tr>
                        <!-- this form is hidden, it fixes the first button that would automatically got to create,
                        now that the first button is hidden, this won't happen. It has no further purpose. Could be 
                        done better though -->
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="visibility:hidden;">
                            <input type="hidden" value="Add" name="retp"/>
                        </form>
                        <?php
                        //get all the items that are on the order and put them in a form so that you can add them to the return
                        foreach($items as $i=>$item){
                            ?>
                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?>">
                                    <td><?php echo $item->getItemNr() ?></td>
                                    <td><?php echo $item->getName() ?></td>
                                    <td><?php echo $item->getType() ?></td>
                                    <td><?php echo $item->getPrice() ?></td>
                                     <td><?php echo $item->getVAT() ?></td>
                                    <td><?php echo $item->getAmount() ?></td>
                                    <td><?php echo $item->getDiscount() ?></td>
                                    <td><?php echo $item->getTotalPrice() ?></td>
                                    <?php
                                        $product = ProductTable::getInstance()->findOneBy('product_number', $item->getItemNr());
                                        if($product != null){
                                            $id = $product->getId(); ?>
                                            <input type="hidden" name="cat" value="<?php echo 'Product' ?>"/>
                                        <?php }
                                        else{
                                            $part = PartTable::getInstance()->findOneBy('part_number', $item->getItemNr());
                                            $id = $part->getId(); ?>
                                            <input type="hidden" name="cat" value="<?php echo 'Material' ?>"/>
                                       <?php }
                                    //put important value's in hidden input so they can be given through when you add a product or part to the return list
                                    ?>
                                    <input type="hidden" name="id" value="<?php echo $id ?>"/><!-- the id of a product or part -->
                                    <input type="hidden" name="pno" value="<?php echo $item->getItemNr() ?>" /><!-- the product or part number -->
                                    <input type="hidden" name="name" value="<?php echo $item->getName() ?>"/><!-- the name of the product or part -->
                                    <input type="hidden" name="type" value="<?php echo $item->getType() ?>"/><!-- the type (if exists) -->
                                    <input type="hidden" name="price" value="<?php echo $item->getPrice() ?>"/><!-- the price for one -->
                                    <input type="hidden" name="discount" value="<?php echo $item->getDiscount() ?>"/><!-- the discount given -->
                                    <input type="hidden" name="amount" value="<?php echo $item->getAmount() ?>"/><!-- the amount that was bought -->
                                    <input type="hidden" name="VAT" value="<?php echo $item->getVAT() ?>"/>
                                    <!-- To make sure we stay on the return page and in this if part we give the order_no we filled in in the previous submit along-->
                                    <input type="hidden" name="order_no" value="<?php echo $order_no ?>"/>
                                    <!-- An imput to fill in how many are being returned and the submit button -->
                                    <td><input type="text" name="return" size="3"/></td>
                                    <td><input type="submit" value="" name="return_p" class="add_bt" title="Add to return order"/></td>
                                </tr>
                            </form><!-- close form of 1 table line that allows you to add something to the return list -->
                            <?php
                        }//closes for each loop
                        ?>
                        </table>
                    </div>
                </fieldset>
                <?php
                
                //check if return_list exist and get all information and display on screen
                if(!empty($_SESSION['return_list'])){
                ?>
                <fieldset>
                    <legend class="bold">Return</legend>
                    <div class="table_div">
                    <table id="return_table">
                        <tr class="bold head_small">
                            <td width="12%">Item No.</td>
                            <td>Name</td>
                            <td>Type</td>
                            <td>Price per Unit (excl. VAT)</td>
                            <td>VAT(%)</td>
                            <td>Quantity</td>
                            <td>Discount</td>
                            <td>Total price</td>
                            <td width="20px"></td>
                        </tr>
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                            <input type="submit" name="empty_return" value="" class="bt_empty" title="Empty entire return order list"/>
                            <input type="hidden" name="order_no" value="<?php echo $order_no ?>"/>
                        </form>
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                            <input type="submit" name="update_return" value="" class="bt_update" title="Update return order"/>
                            <input type="hidden" name="order_no" value="<?php echo $order_no ?>"/>
                            <?php
                            //set the totalprice of what the customer gets back on 0
                            $total_price = 0;
                            //go through session and get information on screen
                            foreach($_SESSION['return_list'] as $value){ ?>
                                <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?>">
                                    <td><?php echo $value['pno'] ?></td>
                                    <td><?php echo $value['name'] ?></td>
                                    <td><?php echo $value['type'] ?></td>
                                    <td><?php echo $value['price'] ?></td>
                                    <td><?php echo $value['VAT'] ?></td>
                                    <td><input type="text" name="return" value="<?php echo $value['return'] ?>" size="3"/></td>
                                    <td><?php echo $value['discount'] ?></td>
                                    <?php
                                    //calculate the total prices per products/parts
                                    $discount_per = '0.'.$value['discount'];
                                    $discount = $value['price'] * $discount_per;
                                    $VAT = '0.'.$value['VAT'];
                                    $VAT_all = 0;
                                    $VAT_total = $VAT * $value['price'];
                                    $total = $value['return'] * $value['price'] - $discount;
                                    $VAT_all = $VAT_all + $VAT_total;
                                    $total_price -= $total;
                                    ?>
                                    <td><?php echo $total ?></td>
                                    <td><input type="submit" value="<?php echo array_search($value, $_SESSION['return_list']) ?>" name="removep" class="bt_delete" title="Remove from return"/></td>
                                    <!-- all hidden inputs -->
                                    <input type="hidden" name="id" value="<?php echo $value['id'] ?>"/>
                                    <input type="hidden" name="name" value="<?php echo $value['name'] ?>"/>
                                    <input type="hidden" name="price" value="<?php echo $value['price'] ?>"/>
                                    <input type="hidden" name="type" value="<?php echo $value['type'] ?>"/>
                                    <input type="hidden" name="pno" value="<?php echo $value['pno'] ?>"/>
                                    <input type="hidden" name="line" value="<?php echo array_search($value, $_SESSION['return_list']) ?>"/>
                                    <input type="hidden" name="amount" value="<?php echo $value['max'] ?>"/>
                                    <input type="hidden" name="discount" value="<?php echo $value['discount'] ?>"/>
                                    
                                </tr>
                                <?php
                            }
                            ?>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td><?php echo $total_price ?></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td>
                                <td class="bold">VAT:</td>
                                <td><?php echo $VAT_all ?></td>
                            </tr>
                            <?php
                            $VAT_all = '-'.$VAT_all;
                            $total_price = $total_price + $VAT_all;
                            ?>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td class="bold">Total:</td>
                                <td><?php echo $total_price ?></td>
                            </tr>
                        </form>
                    </table>
                    </div>
                </fieldset>
                <div class="hidden_csrf">
                    <?php echo $form['_csrf_token']->renderRow(); ?>
                    <?php echo $form['date_recieved']->renderRow(); ?>
                    <?php echo $form['status']->renderRow(); ?>
                </div>
                <?php
                }//close checking if the session return_list exists
                if(!empty($_SESSION['return_list'])){
                    echo '<input type="submit" class="ok ok_margin" value="" title="Confirm return order"/>';
                }
                ?> 
                <input type="hidden" name="totalprice" value="<?php echo $total_price?>"/>
            </form>
            <?php
        }//close if $order_no_query is not null
        else { echo '<p class="submit_error">The entered order number does not exist</p>'; }
    }//closes if order_no is filled in
    else {
        //there was nothing filled in in order no
        echo '<p class="submit_error">Please fill in a order No.</p>';
    }
}
?>
